package com.mspbots.core.teams;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mspbots.core.AbstractBaseController;
import com.mspbots.core.teams.model.BotMessageCacheDTO;
import com.mspbots.core.teams.model.Messages;
import com.mspbots.core.teams.model.MessagesIncontrollable;
import com.mspbots.core.teams.service.MessagesService;
import com.mspbots.cw.request.MessagesDTO;
import com.mspbots.cw.request.TriggerEscalationDTO;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.List;
import java.util.concurrent.Callable;


@RestController
@RequestMapping("/teams/messages")
public class MessagesController extends AbstractBaseController<Messages, MessagesService> {

    @GetMapping("/countMessagesByForRule")
    public Callable countMessagesByForRule(Messages query) {
        return () -> service.countMessagesByForRule(query);
    }

    @GetMapping("/countEscalation")
    public Callable countEscalation(Messages query) {
        return () -> service.countEscalation(query);
    }

    @GetMapping("/list/by-entry/{id}")
    public Callable findByTimeEntryId(@PathVariable Long id) {
        return () -> service.findByTimeEntryId(id);
    }

    @GetMapping("/list/send")
    public Callable listSend(Messages query) {
        return () -> service.listSend(query);
    }

    @PutMapping(path = "/updateBatch")
    public Callable updateBatch(@RequestBody List<Messages> messagesList) {
        return () -> service.updateBatchById(messagesList);
    }

    @GetMapping("report-page")
    public Callable<IPage<MessagesDTO>> reportPage(Page<MessagesDTO> page, MessagesDTO query) {
        return () -> service.reportPage(page, query);
    }

    @GetMapping("/triggerEscalationList")
    public Callable triggerEscalationList(TriggerEscalationDTO query) {
        return () -> service.triggerEscalationList(query);
    }

    @RequestMapping("/count")
    public Callable count(Messages query) {
        QueryWrapper<Messages> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", "manager");
        queryWrapper.eq("tenant_id", query.getTenantId());
        queryWrapper.gt("create_date", LocalDateTime.now().minusDays(7));
        return () -> service.count(queryWrapper);
    }


    @PostMapping("/insertMessages")
    public Callable insertMessages(@RequestBody Messages entity) {
        return () -> service.insertMessages(entity);
    }

    @GetMapping("/incontrollable")
    public Callable incontrollable(MessagesIncontrollable params) {
        return () -> service.incontrollable(params);
    }

    @GetMapping("/list/user")
    public Callable listUser(Messages query) {
        QueryWrapper<Messages> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("tenant_id", query.getTenantId())
                .ge(ObjectUtils.isNotEmpty(query.getCreateDate()), "create_date", query.getCreateDate())
                .select("teams_user_id", "status", "max(expire_date) as expire_date")
                .groupBy("teams_user_id", "status");
        return () -> service.list(queryWrapper);
    }

    @PostMapping("/cache-bot")
    public Callable setMsgCount(@RequestBody BotMessageCacheDTO query) {
        Assert.notNull(query.getScriptId(), "ScriptId must not be null");
        Assert.notNull(query.getUserId(), "UserId must not be null");
        return () -> service.cacheMsgCount(query);
    }

//    @GetMapping("cache/{userId}/{scriptId}")
//    public Callable getMsgCount(@PathVariable("userId") Long userId, @PathVariable("scriptId") Long scriptId) {
//        BotMessageCacheDTO botMessageCacheDTO = new BotMessageCacheDTO();
//        botMessageCacheDTO.setScriptId(scriptId);
//        botMessageCacheDTO.setUserId(userId);
//        return () -> service.getCacheMsgCount(botMessageCacheDTO);
//    }


    @GetMapping("cache")
    public Callable getMsgCount(BotMessageCacheDTO botMessageCacheDTO) {
//        BotMessageCacheDTO botMessageCacheDTO = new BotMessageCacheDTO();
//        botMessageCacheDTO.setScriptId(scriptId);
//        botMessageCacheDTO.setUserId(userId);
        return () -> service.getCacheMsgCount(botMessageCacheDTO);
    }

    @GetMapping("time-entry-bot-count")
    public Callable timeEntryBotCount(BotMessageCacheDTO botMessageCacheDTO) {
//        BotMessageCacheDTO botMessageCacheDTO = new BotMessageCacheDTO();
//        botMessageCacheDTO.setScriptId(scriptId);
//        botMessageCacheDTO.setUserId(userId);
        return () -> service.countTimeEntryBot(botMessageCacheDTO);
    }

    @GetMapping("cache/remove/{userId}")
    public Callable removeCache(@PathVariable Long userId) {
        return () -> service.removeCache(userId);
    }


}
